﻿using A.M.ETO;
using A.M.Storage.Application;
using AutoMapper;
using DotNetCore.CAP;
using Q.M.Finance.Application.IServices;
using Q.M.Finance.Domain.DTO;
using Q.M.Finance.Domain.Entity;
using Q.M.Finance.Infrastruecure.IRepository;
using Q.M.Finance.Infrastruecure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Q.M.Finance.Application.Services
{
    public class To_ticket_managementServices : BaseServices<to_ticket_management, string>, ITo_ticket_managementServices
    {
        private readonly ITo_ticket_managementRepository to_Ticket_ManagementRepository;
        private readonly IMapper mapper;
        private readonly ICapPublisher capPublisher;
        private readonly ITo_ticket_typeRepository to_Ticket_TypeRepository;
        public To_ticket_managementServices(ITo_ticket_typeRepository to_Ticket_TypeRepository, IMapper mapper, ICapPublisher capPublisher,ITo_ticket_managementRepository to_Ticket_ManagementRepository) : base(to_Ticket_ManagementRepository)
        {
            this.to_Ticket_ManagementRepository = to_Ticket_ManagementRepository;
            this.mapper = mapper;
            this.capPublisher = capPublisher;
            this.to_Ticket_TypeRepository = to_Ticket_TypeRepository;
        }
        /// <summary>
        /// 添加到票管理
        /// </summary>
        /// <param name="to_Ticket_ManagementAddDTO"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> GetTo_ticket_managementAddInfo(to_ticket_managementAddDTO to_Ticket_ManagementAddDTO)
        {
            try
            {
                ApiReturn<string> apiReturn = new ApiReturn<string>();
                var to_ticket_management = mapper.Map<to_ticket_management>(to_Ticket_ManagementAddDTO);
                var res = await to_Ticket_ManagementRepository.AddInfor_Async(to_ticket_management);
                if (res > 0)
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "添加成功";
                    //添加成功需要传一个消息队列  传递哪个模块 还有编号
                    string numberName = "到票系统-" + to_Ticket_ManagementAddDTO.to_ticket_managementid;
                    capPublisher.Publish("GetNewNumber", numberName);
                }
                else
                {
                    apiReturn.Code = 400;
                    apiReturn.Message = "添加失败";

                }
                return apiReturn;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改到票管理
        /// </summary>
        /// <param name="to_Ticket_ManagementShowDTO"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> Getbilling_managementUpdateInfo(to_ticket_managementShowDTO to_Ticket_ManagementShowDTO)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var id = to_Ticket_ManagementRepository.GetShowInfor(x => x.to_ticket_managementid.Equals(to_Ticket_ManagementShowDTO.to_ticket_managementid));
            foreach (var item in id)
            {
                item.to_ticket_managementid = to_Ticket_ManagementShowDTO.to_ticket_managementid;
                item.ticket_subject = to_Ticket_ManagementShowDTO.ticket_subject;
                item.to_ticket_typename = to_Ticket_ManagementShowDTO.to_ticket_typename;
                item.date_of_arrival = to_Ticket_ManagementShowDTO.date_of_arrival;
                item.drawer = to_Ticket_ManagementShowDTO.drawer;
                item.invoice_number = to_Ticket_ManagementShowDTO.invoice_number;
                item.amount_received = to_Ticket_ManagementShowDTO.amount_received;
            }
            var res = await to_Ticket_ManagementRepository.UpdInfor_Batch_Async(id);
            if (res > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "修改成功";
            }
            return apiReturn;
        }
        /// <summary>
        /// 反填到票管理
        /// </summary>
        /// <param name="to_ticket_managementid"></param>
        /// <returns></returns>
        public async Task<to_ticket_management> Findto_ticket_managementInfo(string? to_ticket_managementid)
        {
            Expression<Func<to_ticket_management, bool>> i = c => c.to_ticket_managementid == to_ticket_managementid;
            var to_ticket_management = to_Ticket_ManagementRepository.GetShowObj(i);
            return to_ticket_management;
        }

        /// <summary>
        /// 删除到票管理
        /// </summary>
        /// <param name="to_ticket_managementid"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> Deleteto_ticket_management(string? to_ticket_managementid)
        {
            Expression<Func<to_ticket_management, bool>> i = c => c.to_ticket_managementid == to_ticket_managementid;
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var to_ticket_management = to_Ticket_ManagementRepository.GetShowObj(i);
            var list = await to_Ticket_ManagementRepository.DelInfor_Async(to_ticket_management);
            if (list != null)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "删除成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "删除失败!";
            }
            return apiReturn;
        }
        /// <summary>
        /// 到票显示+分页+查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageETO<to_ticket_managementShowDTO> Getbilling_managementInfo(string? to_ticket_managementid, int pageIndex, int pageSize)
        {
            PageETO<to_ticket_managementShowDTO> pageETO = new PageETO<to_ticket_managementShowDTO>();
            Expression<Func<to_ticket_management, bool>> wherexp = i => true;
            if (to_ticket_managementid != null)
            {
                wherexp = i => i.to_ticket_managementid == to_ticket_managementid;
            }
            var incomelist = to_Ticket_ManagementRepository.GetShowInfor(wherexp).ToList();
            var res = to_Ticket_TypeRepository.GetShowInfor();
            var list = (from a in incomelist
                        join b in res on a.to_ticket_typename equals b.to_ticket_typename
                        select new to_ticket_managementShowDTO
                        {
                            to_ticket_managementid = a.to_ticket_managementid,
                            ticket_subject = a.ticket_subject,
                            to_ticket_typename = b.to_ticket_typename,
                            date_of_arrival = a.date_of_arrival,
                            drawer = a.drawer,
                            invoice_number = a.invoice_number,
                            amount_received = a.amount_received,
                        }).ToList();
            pageETO.TotalCount = list.Count();
            pageETO.PageCount = (int)Math.Ceiling(pageETO.TotalCount * 1.0 / pageSize);
            pageETO.Data = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageETO;
        }
    }
}
